package action

import (
	"errors"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
	"github.com/astaxie/beego/session"
	"teamcoop/global"
	. "teamcoop/models"
	"time"
)

func TodoAdd(sid string, text string, level int8) (tid int64, err error) {
	beego.Info("TodoAdd:", sid, "\t", text, "\t", level)
	//get session
	var sessions session.SessionStore
	sessions, err = global.MySessions.GetSessionStore(sid)
	if err != nil {
		err = errors.New("sid error!")
		return
	}

	//从session里get 当前login的用户email
	state := sessions.Get("login_state")
	if "logined" != state {
		err = errors.New("user unlogin!")
		return
	}

	email := sessions.Get("login_email").(string)

	o := orm.NewOrm()
	user := &User{}
	qs := o.QueryTable("user")
	err = qs.Filter("email", email).One(user)
	if err != nil {
		beego.Error(err.Error())
		err = errors.New("logined user is not exsit!")
		return
	}

	beego.Info("TodoAdd with :", user)
	todo := &Todo{
		UserId: user.Id,
		Time:   time.Now(),
		Text:   text,
		Done:   false,
		Level:  level,
	}
	beego.Info("TodoInsert:", todo)

	_, err = o.Insert(todo)
	if err != nil {
		beego.Error(err.Error())
		err = errors.New("add TODO fail!")
		return
	}

	tid = todo.Id

	err = DynamicAddTodo(sid, todo.Id)
	if err != nil {
		beego.Error(err.Error())
		err = errors.New(" dynamic add  fail!")
		return
	}

	return

}

func TodoRemove(sid string, tid int64) (err error) {
	beego.Info("TodoRemove:", sid, tid)
	//get session
	var sessions session.SessionStore
	sessions, err = global.MySessions.GetSessionStore(sid)
	if err != nil {
		beego.Error(err.Error())
		err = errors.New("get sid fail!")
		return
	}

	//从session里get 当前login的用户email
	state := sessions.Get("login_state")
	if "logined" != state {
		return
	}

	email := sessions.Get("login_email").(string)

	o := orm.NewOrm()
	user := &User{}
	qs := o.QueryTable("user")
	err = qs.Filter("email", email).One(user)
	if err != nil {
		err = errors.New("logined user is not exsit!")
		return
	}

	todo := Todo{Id: tid}
	err = o.Read(&todo)
	if err != nil {
		err = errors.New("TODO not exsit!")
		return
	}
	dynamic := &Dynamic{}
	err = o.QueryTable("dynamic").Filter("Type", TYPE_TODO).Filter("TodoId", tid).One(dynamic)
	if err != nil {
		beego.Error(err.Error())
		err = errors.New(" query  dynamic  fail!")
		return
	}
	_, err = o.Delete(dynamic)
	if err != nil {
		err = errors.New(" delete  dynamic  fail!")
		return
	}
	_, err = o.Delete(&todo)
	if err != nil {
		err = errors.New(" delete TODO fail!")
		return
	}
	return
}

func TodoSetLevel(sid string, tid int64, level int8) (err error) {
	beego.Info("TodoSetLevel:", sid, tid, level)
	//get session
	var sessions session.SessionStore
	sessions, err = global.MySessions.GetSessionStore(sid)
	if err != nil {
		beego.Error(err.Error())
		err = errors.New("get sid fail!")
		return
	}

	//从session里get 当前login的用户email
	state := sessions.Get("login_state")
	if "logined" != state {
		beego.Error(err.Error())
		err = errors.New("get sid fail!")
		return
	}

	email := sessions.Get("login_email").(string)

	o := orm.NewOrm()
	user := &User{}
	qs := o.QueryTable("user")
	err = qs.Filter("email", email).One(user)
	if err != nil {
		err = errors.New("logined user is not exsit!")
		return
	}

	_, err = o.Update(&Todo{Id: tid, Level: level}, "Level")
	if err != nil {
		err = errors.New("set TODO level fail!")
		return
	}
	return
}

func TodoSetText(sid string, tid int64, text string) (err error) {
	beego.Info("TodoSetText:", sid, tid, text)
	//get session
	var sessions session.SessionStore
	sessions, err = global.MySessions.GetSessionStore(sid)
	if err != nil {
		return
	}

	//从session里get 当前login的用户email
	state := sessions.Get("login_state")
	if "logined" != state {
		return
	}

	email := sessions.Get("login_email").(string)

	o := orm.NewOrm()
	user := &User{}
	qs := o.QueryTable("user")
	err = qs.Filter("email", email).One(user)
	if err != nil {
		err = errors.New("logined user is not exsit!")
		return
	}

	_, err = o.Update(&Todo{Id: tid, Text: text}, "Text")
	if err != nil {
		err = errors.New("set TODO内容 fail!")
		return
	}
	return
}

func TodoDone(sid string, tid int64) (err error) {
	beego.Info("TodoDone:", sid, tid)
	//get session
	var sessions session.SessionStore
	sessions, err = global.MySessions.GetSessionStore(sid)
	if err != nil {
		return
	}

	//从session里get 当前login的用户email
	state := sessions.Get("login_state")
	if "logined" != state {
		return
	}

	email := sessions.Get("login_email").(string)

	o := orm.NewOrm()
	user := &User{}
	qs := o.QueryTable("user")
	err = qs.Filter("email", email).One(user)
	if err != nil {
		err = errors.New("logined user is not exsit!")
		return
	}

	_, err = o.Update(&Todo{Id: tid, Done: true}, "Done")
	if err != nil {
		err = errors.New("set TODOdone fail!")
		return
	}
	return
}

func TodoUndone(sid string, tid int64) (err error) {
	beego.Info("TodoUndone:", sid, tid)
	//get session
	var sessions session.SessionStore
	sessions, err = global.MySessions.GetSessionStore(sid)
	if err != nil {
		return
	}

	//从session里get 当前login的用户email
	state := sessions.Get("login_state")
	if "logined" != state {
		return
	}

	email := sessions.Get("login_email").(string)

	o := orm.NewOrm()
	user := &User{}
	qs := o.QueryTable("user")
	err = qs.Filter("email", email).One(user)
	if err != nil {
		err = errors.New("logined user is not exsit!")
		return
	}

	_, err = o.Update(&Todo{Id: tid, Done: false}, "Done")
	if err != nil {
		err = errors.New("set TODO undone fail!")
		return
	}
	return
}

func TodoAllDone(sid string) (err error) {
	beego.Info("TodoAllDone:", sid)
	//get session
	var sessions session.SessionStore
	sessions, err = global.MySessions.GetSessionStore(sid)
	if err != nil {
		return
	}

	//从session里get 当前login的用户email
	state := sessions.Get("login_state")
	if "logined" != state {
		return
	}

	email := sessions.Get("login_email").(string)

	o := orm.NewOrm()
	user := &User{}
	qs := o.QueryTable("user")
	err = qs.Filter("email", email).One(user)
	if err != nil {
		err = errors.New("logined user is not exsit!")
		return
	}

	var todoes []*Todo
	qs = o.QueryTable("todo")
	_, err = qs.Filter("UserId", user.Id).All(&todoes)
	if err != nil {
		err = errors.New("query todoes fail!")
		return
	}

	for i := 0; i < len(todoes); i++ {
		todoes[i].Done = true
		o.Update(todoes[i])
	}
	return
}
func TodoAllUnDone(sid string) (err error) {
	beego.Info("TodoAllUnDone:", sid)
	//get session
	var sessions session.SessionStore
	sessions, err = global.MySessions.GetSessionStore(sid)
	if err != nil {
		return
	}

	//从session里get 当前login的用户email
	state := sessions.Get("login_state")
	if "logined" != state {
		return
	}

	email := sessions.Get("login_email").(string)

	o := orm.NewOrm()
	user := &User{}
	qs := o.QueryTable("user")
	err = qs.Filter("email", email).One(user)
	if err != nil {
		err = errors.New("logined user is not exsit!")
		return
	}

	var todoes []*Todo
	qs = o.QueryTable("todo")
	_, err = qs.Filter("UserId", user.Id).All(&todoes)
	if err != nil {
		err = errors.New("query todoes fail!")
		return
	}
	sessions, err = global.MySessions.GetSessionStore(sid)

	for i := 0; i < len(todoes); i++ {
		todoes[i].Done = false
		o.Update(todoes[i])
	}
	return
}
func TodoAllRemoveDone(sid string) (err error) {
	beego.Info("TodoAllRemove:", sid)
	//get session
	var sessions session.SessionStore
	sessions, err = global.MySessions.GetSessionStore(sid)
	if err != nil {
		return
	}

	//从session里get 当前login的用户email
	state := sessions.Get("login_state")
	if "logined" != state {
		return
	}

	email := sessions.Get("login_email").(string)

	o := orm.NewOrm()
	user := &User{}
	qs := o.QueryTable("user")
	err = qs.Filter("email", email).One(user)
	if err != nil {
		err = errors.New("logined user is not exsit!")
		return
	}

	var todoes []*Todo
	qs = o.QueryTable("todo")
	_, err = qs.Filter("UserId", user.Id).Filter("Done", true).All(&todoes)
	if err != nil {
		err = errors.New("query todoes fail!")
		return
	}

	for i := 0; i < len(todoes); i++ {
		dynamic := &Dynamic{}
		err = o.QueryTable("dynamic").Filter("Type", TYPE_TODO).Filter("TodoId", todoes[i].Id).One(dynamic)
		if err != nil {
			beego.Error(err.Error())
			err = errors.New(" query  dynamic  fail!")
			return
		}
		_, err = o.Delete(dynamic)
		if err != nil {
			err = errors.New(" delete  dynamic  fail!")
			return
		}
		o.Delete(todoes[i])
	}
	return
}

func TodoListDone(sid string) (ret_list []*Todo, err error) {
	beego.Info("TodoListDone:", sid)
	//get session
	var sessions session.SessionStore
	sessions, err = global.MySessions.GetSessionStore(sid)
	if err != nil {
		return
	}

	//从session里get 当前login的用户email
	state := sessions.Get("login_state")
	if "logined" != state {
		return
	}

	email := sessions.Get("login_email").(string)

	o := orm.NewOrm()
	user := User{Email: email}
	err = o.QueryTable("user").Filter("email", email).One(&user)
	if err != nil {
		beego.Error(user)
		err = errors.New("logined user is not exsit!")
		return
	}

	qs := o.QueryTable("todo")
	_, err = qs.Filter("UserId", user.Id).Filter("Done", true).All(&ret_list)
	if err != nil {
		err = errors.New("query todoes fail!")
		return
	}

	return
}

func TodoListUndone(sid string) (ret_list []*Todo, err error) {
	beego.Info("TodoListUndone:", sid)
	//get session
	var sessions session.SessionStore
	sessions, err = global.MySessions.GetSessionStore(sid)
	if err != nil {
		return
	}

	//从session里get 当前login的用户email
	state := sessions.Get("login_state")
	if "logined" != state {
		return
	}

	email := sessions.Get("login_email").(string)

	o := orm.NewOrm()
	user := User{Email: email}
	err = o.QueryTable("user").Filter("email", email).One(&user)
	if err != nil {
		beego.Error(user)
		err = errors.New("logined user is not exsit!")
		return
	}

	qs := o.QueryTable("todo")
	_, err = qs.Filter("UserId", user.Id).Filter("Done", false).All(&ret_list)
	if err != nil {
		err = errors.New("query todoes fail!")
		return
	}
	return
}

func TodoListDoneOther(sid string, id int64) (ret_list []*Todo, err error) {
	beego.Info("TodoListDone:", sid)
	//get session
	var sessions session.SessionStore
	sessions, err = global.MySessions.GetSessionStore(sid)
	if err != nil {
		return
	}

	//从session里get 当前login的用户email
	state := sessions.Get("login_state")
	if "logined" != state {
		return
	}

	email := sessions.Get("login_email").(string)

	o := orm.NewOrm()
	user := User{Email: email}
	err = o.QueryTable("user").Filter("email", email).One(&user)
	if err != nil {
		beego.Error(user)
		err = errors.New("logined user is not exsit!")
		return
	}

	qs := o.QueryTable("todo")
	_, err = qs.Filter("UserId", id).Filter("Done", true).All(&ret_list)
	if err != nil {
		err = errors.New("query todoes fail!")
		return
	}

	return
}

func TodoListUndoneOther(sid string, id int64) (ret_list []*Todo, err error) {
	beego.Info("TodoListUndone:", sid)
	//get session
	var sessions session.SessionStore
	sessions, err = global.MySessions.GetSessionStore(sid)
	if err != nil {
		return
	}

	//从session里get 当前login的用户email
	state := sessions.Get("login_state")
	if "logined" != state {
		return
	}

	email := sessions.Get("login_email").(string)

	o := orm.NewOrm()
	user := User{Email: email}
	err = o.QueryTable("user").Filter("email", email).One(&user)
	if err != nil {
		beego.Error(user)
		err = errors.New("logined user is not exsit!")
		return
	}

	qs := o.QueryTable("todo")
	_, err = qs.Filter("UserId", id).Filter("Done", false).All(&ret_list)
	if err != nil {
		err = errors.New("query todoes fail!")
		return
	}
	return
}
